<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.pethospital.service.impl.PetServiceImpl" %>
<%@ page import="com.pethospital.service.PetService" %>
<%@ page import="com.pethospital.entity.Pet" %>
<%@ page import="java.util.List" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.pethospital.util.DBUtil" %>

<!DOCTYPE html>
<html>
<head>
    <title>宠物数据修复工具</title>
    <meta charset="UTF-8">
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .error { color: red; }
        .success { color: green; }
        .info { color: blue; }
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
    </style>
</head>
<body>
    <h1>宠物数据修复工具</h1>
    
    <%
        String action = request.getParameter("action");
        
        if ("fix".equals(action)) {
            out.println("<h2>执行数据修复...</h2>");
            
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            
            try {
                conn = DBUtil.getConnection();
                
                // 查找有问题的宠物数据
                String selectSql = "SELECT pet_id, pet_name, pet_type, breed, owner_id FROM pets WHERE pet_name REGEXP '^[0-9]+$' OR pet_name IS NULL OR pet_name = ''";
                pstmt = conn.prepareStatement(selectSql);
                rs = pstmt.executeQuery();
                
                int fixedCount = 0;
                while (rs.next()) {
                    int petId = rs.getInt("pet_id");
                    String oldName = rs.getString("pet_name");
                    String petType = rs.getString("pet_type");
                    String breed = rs.getString("breed");
                    int ownerId = rs.getInt("owner_id");
                    
                    // 生成新的宠物名称
                    String newName = generatePetName(petType, breed, petId);
                    
                    // 更新宠物名称
                    PreparedStatement updateStmt = conn.prepareStatement("UPDATE pets SET pet_name = ? WHERE pet_id = ?");
                    updateStmt.setString(1, newName);
                    updateStmt.setInt(2, petId);
                    
                    int updated = updateStmt.executeUpdate();
                    if (updated > 0) {
                        out.println("<div class='success'>修复成功 - 宠物ID: " + petId + 
                                   ", 原名称: '" + oldName + "' -> 新名称: '" + newName + "'</div>");
                        fixedCount++;
                    }
                    updateStmt.close();
                }
                
                out.println("<div class='info'>总共修复了 " + fixedCount + " 条宠物数据</div>");
                
            } catch (SQLException e) {
                out.println("<div class='error'>修复失败: " + e.getMessage() + "</div>");
                e.printStackTrace();
            } finally {
                DBUtil.closeAll(conn, pstmt, rs);
            }
        }
        
        // 显示当前宠物数据
        out.println("<h2>当前宠物数据:</h2>");
        
        PetService petService = new PetServiceImpl();
        List<Pet> allPets = petService.getAllPets();
        
        if (allPets != null && !allPets.isEmpty()) {
            out.println("<table>");
            out.println("<tr><th>ID</th><th>名称</th><th>类型</th><th>品种</th><th>主人ID</th><th>状态</th></tr>");
            
            boolean hasProblems = false;
            for (Pet pet : allPets) {
                String nameClass = "";
                boolean isProblem = false;
                
                if (pet.getPetName() == null || pet.getPetName().trim().isEmpty() || 
                    pet.getPetName().matches("\\d+")) {
                    nameClass = "class='error'";
                    isProblem = true;
                    hasProblems = true;
                }
                
                out.println("<tr>");
                out.println("<td>" + pet.getPetId() + "</td>");
                out.println("<td " + nameClass + ">" + pet.getPetName() + (isProblem ? " (有问题)" : "") + "</td>");
                out.println("<td>" + pet.getPetType() + "</td>");
                out.println("<td>" + pet.getBreed() + "</td>");
                out.println("<td>" + pet.getOwnerId() + "</td>");
                out.println("<td>" + pet.getStatus() + "</td>");
                out.println("</tr>");
            }
            out.println("</table>");
            
            if (hasProblems) {
                out.println("<br><a href='?action=fix' style='background-color: #4CAF50; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px;'>修复有问题的宠物数据</a>");
            } else {
                out.println("<div class='success'>所有宠物数据都正常！</div>");
            }
            
        } else {
            out.println("<div class='error'>没有找到任何宠物数据</div>");
        }
    %>
    
    <%!
        private String generatePetName(String petType, String breed, int petId) {
            if (petType != null && breed != null) {
                if ("狗".equals(petType)) {
                    if ("金毛".equals(breed)) return "金毛" + petId;
                    if ("泰迪".equals(breed)) return "泰迪" + petId;
                    if ("哈士奇".equals(breed)) return "哈士奇" + petId;
                    return "狗狗" + petId;
                } else if ("猫".equals(petType)) {
                    if ("英短".equals(breed)) return "英短" + petId;
                    if ("美短".equals(breed)) return "美短" + petId;
                    if ("布偶".equals(breed)) return "布偶" + petId;
                    return "猫咪" + petId;
                }
            }
            return "宠物" + petId;
        }
    %>
    
    <hr>
    <p><a href="${pageContext.request.contextPath}/appointment/add">返回预约页面</a></p>
    <p><a href="${pageContext.request.contextPath}/debug-pets.jsp">查看调试页面</a></p>
</body>
</html>
